﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace FindInIMDB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string[] exts = textBox1.Text.Split(new char[] {';', ' ', '\t', ','});
            if (exts.Length == 0)
                return;
            foreach (string ext in exts)
            {
                if(ext.StartsWith("."))
                    RegisterExt(ext);
                else
                    RegisterExt("." + ext);
            }

        }


        private void RegisterExt(string ext)
        {
            string HandlerName = "FindInIMDB";

            RegistryKey Ext = null, FindInImdb = null, Shell = null, Open = null, Command = null, Handler = null;

            //FindInImdb = Registry.CurrentUser.CreateSubKey(@"HKEY_CURRENT_USER\Software\Classes\FindInIMDB\shell\Find In Imdb\command");


            try
            {
                Ext = Registry.ClassesRoot.CreateSubKey(ext);

                HandlerName = Ext.GetValue("") as string;
                if (!string.IsNullOrEmpty(HandlerName))
                    FindInImdb = Registry.ClassesRoot.CreateSubKey(HandlerName);
                else
                {
                    Ext.SetValue("", "FindInIMDB");
                    FindInImdb = Registry.ClassesRoot.CreateSubKey("FindInIMDB");
                }
                Shell = FindInImdb.CreateSubKey("shell");
                Open = Shell.CreateSubKey("Find in IMDB");
                Command = Open.CreateSubKey("command");
                Command.SetValue("", Application.ExecutablePath + " imdb %1");

                MessageBox.Show("已注册扩展名 " + ext);
            }
            catch (UnauthorizedAccessException e)
            {
                MessageBox.Show("权限不足，请以管理员身份启动本程序。");
            }
            finally
            {
                if(Command != null)
                    Command.Close();
                if(Open !=null)
                    Open.Close();
                if(Shell != null)
                    Shell.Close();
                if(FindInImdb != null)
                    FindInImdb.Close();
                if(Ext != null)
                    Ext.Close();
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] exts = textBox1.Text.Split(new char[] { ';', ' ', '\t', ',' });
            if (exts.Length == 0)
                return;
            foreach (string ext in exts)
            {
                if (ext.StartsWith("."))
                    UnregisterExt(ext);
                else
                    UnregisterExt("." + ext);
            }
        }

        private void UnregisterExt(string ext)
        {
            string HandlerName = "FindInIMDB";

            RegistryKey Ext = null, FindInImdb = null, Shell = null;

            //FindInImdb = Registry.CurrentUser.CreateSubKey(@"HKEY_CURRENT_USER\Software\Classes\FindInIMDB\shell\Find In Imdb\command");


            try
            {
                Ext = Registry.ClassesRoot.CreateSubKey(ext);

                HandlerName = Ext.GetValue("") as string;
                if ("FindInIMDB" == HandlerName)
                    Ext.SetValue("", "");

                if (!string.IsNullOrEmpty(HandlerName))
                    FindInImdb = Registry.ClassesRoot.CreateSubKey(HandlerName);
                else
                    FindInImdb = Ext.CreateSubKey("FindInIMDB");
                Shell = FindInImdb.CreateSubKey("shell");
                Shell.DeleteSubKeyTree("Find in IMDB");
                MessageBox.Show("已卸载扩展名 " + ext);
            }
            catch (UnauthorizedAccessException e)
            {
                MessageBox.Show("权限不足，请以管理员身份启动本程序。");
            }
            finally
            {
                if (Shell != null)
                    Shell.Close();
                if (FindInImdb != null)
                    FindInImdb.Close();
                if (Ext != null)
                    Ext.Close();
            }

        }
    }
}
